package com.i2works.smartluolong.manage.controller.admin;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleDO;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminRoleService;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.base.URLConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理角色
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminRoleController
 * @date 2015年11月13日 下午9:57:36
 */
@Controller
@RequestMapping("/adminRole")
public class AdminRoleController extends BaseModule {

    private final IAdminRoleService adminRoleService;

    @Autowired
    public AdminRoleController(IAdminRoleService adminRoleService) {
        this.adminRoleService = adminRoleService;
    }

    /**
     * 系统角色管理页面
     */
    @RequestMapping(value = "/view/index")
    public String index() {
        logger.info("系统角色管理页面");
        return URLConstant.Admin.ROLE_URL;
    }

    /**
     * 角色列表
     */
    @RequestMapping(value = "/view/api")
    @ResponseBody
    public Result query(@ModelAttribute AdminRoleQuery query) {
        Result result = new Result();
        try {
            query.setIsPage(true);//启用分页
            int total = adminRoleService.queryCount(query);
            List<AdminRoleDO> queryAdminRoleList = Lists.newArrayList();
            if (total > 0) {
                queryAdminRoleList = adminRoleService.queryList(query);
            }
            result.setTotal(total);
            result.setRows(queryAdminRoleList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 用户下拉角色列表
     */
    @RequestMapping(value = "/view/roleSelect")
    @ResponseBody
    public Result querySelect(@ModelAttribute AdminRoleQuery query) {
        Result result = new Result();
        try {
            query.setStatus(1);//只显示启用的
            query.setLimit(100L);
            List<AdminRoleDO> queryList = adminRoleService.queryList(query);
            result.setTotal(queryList.size());
            result.setRows(queryList);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 新增编辑角色跳转
     */
    @RequestMapping(value = "/save/form")
    public String savePage() {
        logger.info("角色新增编辑");
        return URLConstant.Admin.ROLE_FORM_URL;
    }


    /**
     * 添加、编辑系统角色
     */
    @RequestMapping(value = "/save/action", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@ModelAttribute AdminRoleDO adminRole, @RequestParam(value = "ids", required = false) String ids) {
        Result result = new Result();
        try {
            List<String> resList;
            if (StringUtils.isNotBlank(ids)) {
                resList = BaseUtils.strToList(ids, null);
                if (null != resList && resList.size() > 0) {
                    adminRole.setResList(resList);
                }
            }
            adminRole.setUpdateName(getUserName());
            adminRoleService.save(adminRole);

        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SAVE_ERROR.getMsg());
            System.out.println(result);
        }
        return result;
    }


    /**
     * 某个系统角色（角色名和标示）是否存在验证
     */
    @RequestMapping(value = "/view/check", method = RequestMethod.POST)
    @ResponseBody
    public boolean queryCheck(@ModelAttribute AdminRoleQuery query) {
        boolean result = false;
        try {
            int count = adminRoleService.queryCheck(query);
            if (count <= 0) {
                result = true;
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return result;
    }

    /**
     * 更新角色状态
     * 备注：附加更新角色与用户关系表
     */
    @RequestMapping(value = "/status/action", method = RequestMethod.POST)
    @ResponseBody
    public Result updateStatus(@ModelAttribute AdminRoleDO adminRole) {
        Result result = new Result();
        try {
            adminRole.setUpdateName(getUserName());
            adminRoleService.updateStatus(adminRole);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 批量更新角色状态
     * 备注：附加更新角色与用户关系表
     */
    @RequestMapping(value = "/status/batchAction", method = RequestMethod.POST)
    @ResponseBody
    public Result updateBatchStatus(@RequestParam("idsList[]") List<Integer> idsList, Integer status) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("idsList", idsList);
                map.put("updateName", getUserName());
                map.put("status", status);
                adminRoleService.updateBatchStatus(map);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 删除
     *
     * @param id
     * @return result
     */
    @RequestMapping(value = "/delete/action", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam Integer id) {
        Result result = new Result();
        try {
            adminRoleService.delete(id);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @param idsList
     * @return result
     */
    @RequestMapping(value = "/delete/batchAction", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteBatch(@RequestParam("idsList[]") List<Integer> idsList) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                adminRoleService.deleteBatch(idsList);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.DELETE_ERROR.getMsg());
        }
        return result;
    }

}
